package de.kfzsuche.hilfsklassen;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatenbankManager extends SQLiteOpenHelper {

	private static AssetManager asset;
	private static final String DB_NAME = "kfz.db";
	private static final int DB_VERSION = 1;
	private String[] create;/*"CREATE TABLE test("+
		"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
		" name TEXT NOT NULL);";*/
	private String[] insert;
	
	//private static final String DATEN_INSERT = new AssetDateiLesen(context, "/inser_into.sql").getAssetLesen();
	private static final String TABELLEN_LOESCHEN = "DROP TABLE IF EXISTS test";
	//new AssetDateiLesen(context, "/drop_kfzdb.sql").getAssetLesen();		
	
	//Konstruktor
		public DatenbankManager(Context context) 
		{
			super(context, DB_NAME, null, DB_VERSION);
					
			this.create = sToSqlArray(assetLesen(context, "create_kfzdb.sql"));
			this.insert = sToSqlArray(assetLesen(context, "insert_into.sql"));
		}
		
		//erstellt die Datenbank wenn sie noch nicht existiert
		public void onCreate(SQLiteDatabase db)
		{
			for(int j=0;j < create.length;j++){
				db.execSQL(create[j]);
			}
			for(int k=0;k < insert.length;k++){
				db.execSQL(insert[k]);
			}
			/*db.execSQL(KLASSEN_CREATE);
			db.execSQL(DATEN_INSERT);*/
		}

		//aktualisiert die Datenbank
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(TABELLEN_LOESCHEN);
			onCreate(db);
		}
		
		public String getString(){
			return "";//KLASSEN_CREATE;
		}	

		    
	    private String assetLesen(Context context, String pfad)
	    {
	    	String s = new String();
	    	
	    	try
			{
				AssetManager mgr = context.getAssets();
				InputStream in = mgr.open(pfad);			
				InputStreamReader isr = new InputStreamReader(in);
			
				BufferedReader fileInBuffer = new BufferedReader(isr);
				String zeile;
				
				try
				{
					while ((zeile = fileInBuffer.readLine()) != null)
					{
						s += zeile;
					}
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				finally
				{
					fileInBuffer.close();
				}
			} 
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    	
			return s;
	    }
	    
	    public String[] sToSqlArray(String s){
	    	int counter =0;
	    	for (int i=0;i<s.length();i++)
	    	{
	    	     if (s.charAt(i) == ';') counter++;
	    	}
	    	   
	    	String sArray[] = new String[counter];
	    	int i=0;
	    	int j=0;
	    	int index=0;
	    	while((index = s.indexOf(";", i)) != -1){
	    		sArray[j]=s.substring(i, index+1);
	    		j++;
	    		i=index+1;
	    	}
	    	return sArray;
	    }
	   /* public String getCreate(){
	    	return create;
	    }*/
	
}
